package com.googlecode.yonggemovie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.googlecode.yonggemovie.domain.Movie;
import com.googlecode.yonggemovie.utils.db.BaseDao;
import com.googlecode.yonggemovie.utils.db.MultiRowMapper;
import com.googlecode.yonggemovie.utils.db.SingleRowMapper;


/**
 * 总体使用：数据的操作，如果需要检索数据库的，不设计service层，直接在界面中调用DAO取数据。
 * DAO设计成单例模式并继承BaseDao
 * 外部调用举例：MovieDao.getInstance()
 * @author Kobe Su,2009-12-4
 */
public class MovieDao extends BaseDao {
	
	private static MovieDao movieDao;
	static {
		movieDao = new MovieDao();
	}
	
	//防止实例化
	private MovieDao() {}
	
	public static MovieDao getInstance() {
		return movieDao;
	}
	
	
	// 将数据库执行结果集转化为entity对象
	private Movie resultSetToEntity(ResultSet rs) throws SQLException {
		Movie movie = new Movie();
		
		movie.setId(rs.getString("id"));
		movie.setName(rs.getString("name"));
		movie.setChineseName(rs.getString("chinese_name"));
		movie.setDirector(rs.getString("director"));
		movie.setWriters(rs.getString("writers"));
		movie.setStarring(rs.getString("starring"));
		movie.setGenre(rs.getString("genre"));
		movie.setReleaseDate(rs.getString("release_date"));
		movie.setUserRating(rs.getInt("user_rating"));
		movie.setPlot(rs.getString("plot"));
		movie.setFilePath(rs.getString("file_path"));
		movie.setCertification(rs.getString("certification"));
		movie.setRuntime(rs.getInt("runtime"));
		movie.setCountry(rs.getString("country"	));
		movie.setLanguage(rs.getString("language"));
		movie.setColor(rs.getString("color"));
		movie.setCompany(rs.getString("company"));
		
		movie.setCreateTime(rs.getDate("create_time"));
		movie.setUpdateTime(rs.getDate("update_time"));

		return movie;
	}
	
	
	
	/**
	 * 调用举例
	 * @param director
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Movie> getMovieByDirector(String director) {
		String sql = "select * from m_movie where director=?";
		List<Movie> list = this.queryForList(sql,new Object[]{director}, new MultiRowMapper(){
			public Object mapRow(ResultSet rs) throws SQLException {			
				return resultSetToEntity(rs);
			}
			
		});
		
		return list;
	}
	
	/**
	 * 电影的模糊查询 ，前面一个是字段名称， 后面一个是值
	 * @param field
	 * @param value
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Movie> fuzzySearchMovie(String field, String value) {
		String sql = "select * from m_movie where "+field+" like  '%"+value+"%'";
		List<Movie> list = this.queryForList(sql,null, new MultiRowMapper(){
			public Object mapRow(ResultSet rs) throws SQLException {			
				return resultSetToEntity(rs);
			}
			
		});
		
		return list;
	}
	
	public Movie findMovie(String id) {
		String sql="select * from m_movie where id=?";
		Object obj=this.query(sql,new Object[]{id}, new SingleRowMapper() {
			@Override
			public Object mapRow(ResultSet rs) throws SQLException {
				return resultSetToEntity(rs);
			}
		});
		if (obj == null)  return null;
		
		return (Movie)obj;
		
	}
	
	
	/**
	 * 返回movie的uuid
	 * @param movie
	 * @return
	 */
	public String saveMovie(Movie movie) {
		
		String uuid=UUID.randomUUID().toString();
		Date createTime=new Date();
		String sql="insert into  m_movie(id, name, chinese_name, director, writers, starring, genre, "
			+"release_date, user_rating, plot, file_path, certification, runtime, country, "
			+"language, color, company,create_time ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		
		this.update(sql, new Object[] {
				uuid, movie.getName(), movie.getChineseName(),
				movie.getDirector(),movie.getWriters(),movie.getStarring(),
				movie.getGenre(), movie.getReleaseDate(), movie.getUserRating(),
				movie.getPlot(),movie.getFilePath(), movie.getCertification(),
				movie.getRuntime(), movie.getCountry(), movie.getLanguage(),
				movie.getColor(), movie.getCompany()  ,createTime
				
		});
		return uuid;
		
	}
	
	public void removeMovie(String id) {
		String sql="delete from m_movie where id=?";
		this.update(sql, new Object[]{id} );
	}
	
	public void removeAllMovie() {
		String sql="delete from m_movie ";
		this.update(sql);
	}
	
	public void updateMovie(Movie movie) {
		
		Date updateTime=new Date();
		String sql="update m_movie  set"
			+" name=?, chinese_name=?, director=?, writers=?,"
			+" starring=?, genre=?, release_date=?, user_rating=?, "
			+" plot=?, file_path=?, certification=?, runtime=?, "
			+" country=?, language=?, color=?, company =?,"
			+" update_time=? where id = ?";
		
		this.update(sql, new Object[] {
				movie.getName(), movie.getChineseName(),
				movie.getDirector(),movie.getWriters(),movie.getStarring(),
				movie.getGenre(), movie.getReleaseDate(), movie.getUserRating(),
				movie.getPlot(),movie.getFilePath(), movie.getCertification(),
				movie.getRuntime(), movie.getCountry(), movie.getLanguage(),
				movie.getColor(), movie.getCompany(), updateTime,
				movie.getId()  
		});

	}
	
	public static void main(String[] args) {
		/*
		List<Movie> movieList = MovieDao.getInstance().getMovieByDirector("kobe");
		for(Movie movie : movieList) {
			System.out.println(movie.getChineseName());
		}
		*/
		String uuid=UUID.randomUUID().toString();
		System.out.println(uuid);
	}
	
	
}
